package com.hwua.dao.Impl;

import com.hwua.Util.C3P0Util;
import com.hwua.dao.INewsDao;
import com.hwua.entity.News;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

public class NewsDaoImpl implements INewsDao {
    private QueryRunner queryRunner=null;

    public NewsDaoImpl() {
        queryRunner=new QueryRunner(C3P0Util.getCpds());
    }

    @Override
    public List<News> queryAll() throws Exception {
        String sql="select * from amz_news";
        return queryRunner.query(sql,new BeanListHandler<News>(News.class));
    }

    @Override
    public News query(Long id) throws Exception {
        String sql="select * from amz_news where id=?";
        Object[] args={id};
        return queryRunner.query(sql,new BeanHandler<News>(News.class),args);
    }
}
